#include "log.h"

#include <stdio.h>
#include <stdarg.h>

#include <Windows.h>

HANDLE g_stdout = INVALID_HANDLE_VALUE;
FILE* g_file = NULL;

#ifndef _DEBUG
#define _DEBUG
#endif

bool Log_Init()
{
#ifdef _DEBUG
	if (!AllocConsole())
	{
		return false;
	}

	g_stdout = GetStdHandle(STD_OUTPUT_HANDLE);

	g_file = fopen("log.txt", "w+");
	if (g_file == NULL)
	{
		return false;
	}

#endif
	return true;
}

void Log_Shutdown()
{
#ifdef _DEBUG
	fclose(g_file);
	FreeConsole();
#endif
}

void Log_Printf(const char* fmt, ...)
{
#ifdef _DEBUG
	va_list argList;
	va_start(argList, fmt);

	char buffer[1024];
	vsprintf(buffer, fmt, argList);
	va_end(argList);

	DWORD written = -1;
	WriteConsoleA(g_stdout, buffer, strlen(buffer), &written, NULL);

	fprintf(g_file, buffer);
#endif
}
